package com.heima.wemedia.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.WmSensitivePageRepDto;
import com.heima.model.wemedia.entity.WmSensitive;
import com.heima.wemedia.dao.WmSensitiveDao;
import com.heima.wemedia.service.WmSensitiveService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Date;

/**
 * 自媒体敏感词表(WmSensitive)表服务实现类
 *
 * @author makejava
 * @since 2022-04-22 21:02:34
 */
@Service("wmSensitiveService")
public class WmSensitiveServiceImpl implements WmSensitiveService {
    @Resource
    private WmSensitiveDao wmSensitiveDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WmSensitive queryById(Long id) {
        return this.wmSensitiveDao.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param wmSensitive 实例对象
     * @return 实例对象
     */
    @Override
    public WmSensitive insert(WmSensitive wmSensitive) {
        this.wmSensitiveDao.insert(wmSensitive);
        return wmSensitive;
    }

    /**
     * 修改数据
     *
     * @param wmSensitive 实例对象
     * @return 实例对象
     */
    @Override
    public WmSensitive update(WmSensitive wmSensitive) {
        this.wmSensitiveDao.update(wmSensitive);
        return this.queryById(wmSensitive.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.wmSensitiveDao.deleteById(id) > 0;
    }

    /**
     * 新增敏感词
     *
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult save(WmSensitive wmSensitive) {
        if (wmSensitive == null || StringUtils.isBlank(wmSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        wmSensitive.setCreatedTime(new Date());
        wmSensitive.setUpdatedTime(new Date());
        wmSensitiveDao.insert(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 列表展示
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmSensitivePageRepDto dto) {
        //1.参数校验
        if (dto==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();
        //2.封装查询条件
        String name = dto.getName();
        //开启分页查询
        PageHelper.startPage(dto.getPage(), dto.getSize());
        Page<WmSensitive> page = wmSensitiveDao.queryAll(name);
        ResponseResult result = new PageResponseResult(page.getPages(), page.getPageSize(), (int) page.getTotal());
        result.setData(page.getResult());
        return result;
    }
}
